/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved.
 *
 */
import { TaskNames } from './common/task-names.js';
import { TargetTaskService } from './service/target-task-service.js';
import { OhosHapTask } from './task/ohos-hap-task.js';
import Task = TaskNames.Task;
import { InputFile, OutputFile } from '@ohos/hvigor';
import fse from 'fs-extra';
import path from 'path';
import { PathInfo } from '../common/path-info.js';
import { CommonConst } from '../const/common-const.js';

export class GeneratePkgModuleJson extends OhosHapTask {
  @InputFile() protected readonly jsonPath: string;
  @OutputFile() protected readonly packageHapJsonPath: string;

  constructor(taskService: TargetTaskService) {
    super(taskService, Task.GENERATE_PKG_MODULE_JSON);
    const targetData = this.targetService.getTargetData();
    const pathInfo: PathInfo = targetData.getPathInfo() as PathInfo;
    this.jsonPath = path.resolve(pathInfo.getIntermediatesRes(), CommonConst.MODULE_JSON);
    this.packageHapJsonPath = path.resolve(this.pathInfo.getPackageHapProductPath(), CommonConst.MODULE_JSON);
  }

  protected async doTaskAction(): Promise<void> {
    const packageHapProductPath = this.pathInfo.getPackageHapProductPath();
    if (!fse.existsSync(packageHapProductPath)) {
      fse.ensureDirSync(packageHapProductPath);
    }
    if (fse.existsSync(this.jsonPath)) {
      fse.copyFileSync(this.jsonPath, this.packageHapJsonPath);
    }
  }

  initTaskDepends() {}
}
